/*
 * @Author: your name
 * @Date: 2020-07-28 09:35:20
 * @LastEditTime: 2020-09-03 13:15:39
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \admin\src\comment\loading.js
 */
// 注册VUE 全局loding
import Vue from 'vue'
import loadingComponent from './loading.vue'
const LoadingConstructor = Vue.extend(loadingComponent)
const instance = new LoadingConstructor({
  el: document.createElement('div')
})
instance.requestingCount = 0
instance.isShowLoading = false // 默认隐藏
const loading = {
  show() { // 显示方法
    instance.requestingCount++
    instance.isShowLoading = true
    document.body.appendChild(instance.$el)
  },
  hide() { // 隐藏方法
    setTimeout(() => {
      instance.isShowLoading = false
      instance.requestingCount--
      if (instance.requestingCount < 0) {
        instance.requestingCount = 0
      }
    }, 0)
  },
  flag() {
    instance.isFlag = true
    setTimeout(() => {
      instance.isFlag = false
    }, 100)
  },
  isCount() {
    return instance.requestingCount
  },
  isreduce(count) {
    instance.requestingCount = instance.requestingCount - count
    if (instance.requestingCount < 0) {
      instance.requestingCount = 0
    }
  }
}
export default {
  install() {
    if (!Vue.$loading) {
      Vue.$loading = loading
    }
    Vue.mixin({
      created() {
        this.$loading = Vue.$loading
      }
    })
  }
}